package breeze.pixel.weather;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import breeze.pixel.weather.apps_util.utils.MDialog;
import breeze.pixel.weather.apps_util.utils.MToast;
import breeze.pixel.weather.apps_util.utils.appsettings.AppSettings;

public abstract class BaseView extends Activity {

    public static final String TAG = "BaseView";

    @SuppressLint("StaticFieldLeak")
    public static Context mBaseContext;

    public static ExecutorService mExecutorService;

    public AppSettings sets;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBaseContext = getApplicationContext();
        //实现状态栏沉浸
        getWindow().getDecorView().setSystemUiVisibility(1280);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        //初始化设置类
        sets = AppSettings.getInstance(this);
    }

    public <T> T find(int viewId) {
        return (T) findViewById(viewId);
    }

    public abstract void init();

    public abstract void initData();

    public void toast(final String message) {
        runOnUiThread(() -> MToast.show(BaseView.this, message));
    }

    /**
     * @return 线程池
     * @description 获取线程
     */
    public static ExecutorService getExec() {
        if (mExecutorService == null) {
            mExecutorService = Executors.newSingleThreadExecutor();
        }
        return mExecutorService;
    }

    public void showDialog(Context context, String title, String message) {
        runOnUiThread(() -> {
            MDialog dialog = new MDialog(context);
            dialog.setTitle(title);
            dialog.setMessage(message);
            dialog.setPositiveButton("确定", null);
            dialog.show();
        });
    }

    public static String getFileProvider() {
        return mBaseContext.getPackageName() + ".fileprovider";
    }

    public boolean checkPermission(String permission) {
        return ActivityCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
    }
}
